home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr34 / blowpas.zip / BLOWFISH.PAS < prev    next >
Pascal/Delphi Source File  |  1995-01-02  |  37KB  |  736 lines

  1. UNIT BlowFish;
  2.  
  3. {$DEFINE Include_Asm}
  4.  
  5. {If you don't have an Intel 80386 processor or higher, delete this
  6.  compiler directive}
  7.  
  8. (*                                BLOWFISH.PAS
  9.  
  10.                                  Version 1.1b
  11.  
  12.                                31 December 1994
  13.  
  14.         Original  C  code  written  by  Bruce  Schneier,  translated to
  15.         Turbo  Pascal with some additional changes  by Walter van Holst
  16.         with some debugging help from  Erik Cumps. Since Bruce Schneier
  17.         placed the algorithm  in the public  domain this implementation
  18.         should be as well.
  19.  
  20.         The additional changes are as following:
  21.  
  22.             - Left out the part of the key initialization routine which
  23.               loaded a data file containing a random  seed, exactly the
  24.               same random seed is included in an constant. This is done
  25.               to make it easier to use this source for another compiler
  26.               than Borland  Pascal or Turbo Pascal. In the case you are
  27.               using this code on another  platform than MS-DOS and/or a
  28.               compiler which is not compatible with Turbo Pascal you'll
  29.               have to rewrite the F function entirely and should take a
  30.               look at the InitBlowfish routine as well. Apart from this
  31.               only the LongInt type variable might be platform/compiler
  32.               dependent, the rest of this code is standard Pascal.
  33.  
  34.               However, you should outcomment  any reference to the i386
  35.               asm code included with  this package. Since  Jeroen did a
  36.               nice job in commenting his code, it should be easy for an
  37.               experienced M68xxx  assembler programmer  to port it to a
  38.               Motorola  platform, again  apart from  the little and big
  39.               endian  story. I would strongly discourage any  effort to
  40.               port anything of this to a  processor  unable to handle 32
  41.               bits integers in one instruction.
  42.  
  43.             - The original source  code in C as published in Dr. Dobb's
  44.               Journal of April 1994 used one variable containing the P-
  45.               array  and another variable containing  the four S-boxes.
  46.               In  this implementation  only the datatypes of  these two
  47.               are defined. This enables the user to initialize multiple
  48.               keys concurrently,  but requires  the calling routines to
  49.               have the used variables defined themselves.
  50.  
  51.             - The three most used routines (the F function, BlowEncrypt
  52.               BlowDecrypt)  are also implemented in TASM  thanks to the
  53.               efforts of Jeroen Pluimers.  The optimization of the code
  54.               resulted in a  performance increase of about 6000% If you
  55.               use the compiler  directive $Define Include_Asm and  have
  56.               all the .OBJ files in the right directory the performance
  57.               should be at least 256 Kbit/s using an i386DX33 processor
  58.               or better.
  59.  
  60.             The author of the Blowfish algorithm can be reached via the
  61.             internet  as schneier@chinet.com, the authors of  this unit
  62.             have the following Fidonet addresses:
  63.  
  64.                                Erik Cumps       2:292/608.24
  65.                                Walter van.Holst 2:281/201.4
  66.                                Jeroen Pluimers  2:281/256
  67.  
  68.             Thanks to John Lots for helping with the first steps to im-
  69.             plement crucial parts in i386 assembler.
  70. *)
  71.  
  72. INTERFACE
  73.  
  74. TYPE PArray = ARRAY [1..18] OF LongInt;
  75.      SBox   = ARRAY [1..4, 0..255] OF LongInt;
  76.      LArray = ARRAY [0..3] OF BYTE;
  77.      PLArray= ^LArray;
  78.  
  79. CONST HighestNumber=294967296;
  80.  
  81. CONST RandomSeed: ARRAY [0..5005] OF BYTE = (
  82.         36,  63, 106, 136, 133, 163,   8, 211,  19,  25, 138,
  83.         46,   3, 112, 115,  68, 164,   9,  56,  34,  41, 159,
  84.         49, 208,   8,  46, 250, 152, 236,  78, 108, 137,  69,
  85.         40,  33, 230,  56, 208,  19, 119, 190,  84, 102, 207,
  86.         52, 233,  12, 108, 192, 172,  41, 183, 201, 124,  80,
  87.        221,  63, 132, 213, 181, 181,  71,   9,  23, 146,  22,
  88.        213, 217, 137, 121, 251,  27, 209,  49,  11, 166, 152,
  89.        223, 181, 172,  47, 253, 114, 219, 208,  26, 223, 183,
  90.        184, 225, 175, 237, 106,  38, 126, 150, 186, 124, 144,
  91.         69, 241,  44, 127, 153,  36, 161, 153,  71, 179, 145,
  92.        108, 247,   8,   1, 242, 226, 133, 142, 252,  22,  99,
  93.        105,  32, 216, 113,  87,  78, 105, 164,  88, 254, 163,
  94.        244, 147,  61, 126,  13, 149, 116, 143, 114, 142, 182,
  95.         88, 113, 139, 205,  88, 130,  21,  74, 238, 123,  84,
  96.        164,  29, 194,  90,  89, 181, 156,  48, 213,  57,  42,
  97.        242,  96,  19, 197, 209, 176,  35,  40,  96, 133, 240,
  98.        202,  65, 121,  24, 184, 219,  56, 239, 142, 121, 220,
  99.        176,  96,  58,  24,  14, 108, 158,  14, 139, 176,  30,
  100.        138,  62, 215,  21, 119, 193, 189,  49,  75,  39, 120,
  101.        175,  47, 218,  85,  96,  92,  96, 230,  85,  37, 243,
  102.        170,  85, 171, 148,  87,  72, 152,  98,  99, 232,  20,
  103.         64,  85, 202,  57, 106,  42, 171,  16, 182, 180, 204,
  104.         92,  52,  17,  65, 232, 206, 161,  84, 134, 175, 124,
  105.        114, 233, 147, 179, 238,  20,  17,  99, 111, 188,  42,
  106.         43, 169, 197,  93, 116,  24,  49, 246, 206,  92,  62,
  107.         22, 155, 135, 147,  30, 175, 214, 186,  51, 108,  36,
  108.        207,  92, 122,  50,  83, 129,  40, 149, 134, 119,  59,
  109.        143,  72, 152, 107,  75, 185, 175, 196, 191, 232,  27,
  110.        102,  40,  33, 147,  97, 216,   9, 204, 251,  33, 169,
  111.        145,  72, 124, 172,  96,  93, 236, 128,  50, 239, 132,
  112.         93,  93, 233, 133, 117, 177, 220,  38,  35,   2, 235,
  113.        101,  27, 136,  35, 137,  62, 129, 211, 150, 172, 197,
  114.         15, 109, 111, 243, 131, 244,  66,  57,  46,  11,  68,
  115.        130, 164, 132,  32,   4, 105, 200, 240,  74, 158,  31,
  116.        155,  94,  33, 198, 104,  66, 246, 233, 108, 154, 103,
  117.         12, 156,  97, 171, 211, 136, 240, 106,  81, 160, 210,
  118.        216,  84,  47, 104, 150,  15, 167,  40, 171,  81,  51,
  119.        163, 110, 239,  11, 108,  19, 122,  59, 228, 186,  59,
  120.        240,  80, 126, 251,  42, 152, 161, 241, 101,  29,  57,
  121.        175,   1, 118, 102, 202,  89,  62, 130,  67,  14, 136,
  122.        140, 238, 134,  25,  69, 111, 159, 180, 125, 132, 165,
  123.        195,  59, 139,  94, 190, 224, 111, 117, 216, 133, 193,
  124.         32, 115,  64,  26,  68, 159,  86, 193, 106, 166,  78,
  125.        211, 170,  98,  54,  63, 119,   6,  27, 254, 223, 114,
  126.         66, 155,   2,  61,  55, 208, 215,  36, 208,  10,  18,
  127.         72, 219,  15, 234, 211,  73, 241, 192, 155,   7,  83,
  128.        114, 201, 128, 153,  27, 123,  37, 212, 121, 216, 246,
  129.        232, 222, 247, 227, 254,  80,  26, 182, 121,  76,  59,
  130.        151, 108, 224, 189,   4, 192,   6, 186, 193, 169,  79,
  131.        182,  64, 159,  96, 196,  94,  92, 158, 194,  25, 106,
  132.         36,  99, 104, 251, 111, 175,  62, 108,  83, 181,  19,
  133.         57, 178, 235,  59,  82, 236, 111, 109, 252,  81,  31,
  134.        155,  48, 149,  44, 204, 129,  69,  68, 175,  94, 189,
  135.          9, 190, 227, 208,   4, 222,  51,  74, 253, 102,  15,
  136.         40,   7,  25,  46,  75, 179, 192, 203, 168,  87,  69,
  137.        200, 116,  15, 210,  11,  95,  57, 185, 211, 251, 219,
  138.         85, 121, 192, 189,  26,  96,  50,  10, 214, 161,   0,
  139.        198,  64,  44, 114, 121, 103, 159,  37, 254, 251,  31,
  140.        163, 204, 142, 165, 233, 248, 219,  50,  34, 248,  60,
  141.        117,  22, 223, 253,  97, 107,  21,  47,  80,  30, 200,
  142.        173,   5,  82, 171,  50,  61, 181, 250, 253,  35, 135,
  143.         96,  83,  49, 123,  72,  62,   0, 223, 130, 158,  92,
  144.         87, 187, 202, 111, 140, 160,  26, 135,  86,  46, 223,
  145.         23, 105, 219, 213,  66, 168, 246,  40, 126, 255, 195,
  146.        172, 103,  50, 198, 140,  79,  85, 115, 105,  91,  39,
  147.        176, 187, 202,  88, 200, 225, 255, 163,  93, 184, 240,
  148.         17, 160,  16, 250,  61, 152, 253,  33, 131, 184,  74,
  149.        252, 181, 108,  45, 209, 211,  91, 154,  83, 228, 121,
  150.        182, 248,  69, 101, 210, 142,  73, 188,  75, 251, 151,
  151.        144, 225, 221, 242, 218, 164, 203, 126,  51,  98, 251,
  152.         19,  65, 206, 228, 198, 232, 239,  32, 202, 218,  54,
  153.        119,  76,   1, 208, 126, 158, 254,  43, 241,  31, 180,
  154.        149, 219, 218,  77, 174, 144, 145, 152, 234, 173, 142,
  155.        113, 107, 147, 213, 160, 208, 142, 209, 208, 175, 199,
  156.         37, 224, 142,  60,  91,  47, 142, 117, 148, 183, 143,
  157.        246, 226, 251, 242,  18,  43, 100, 136, 136, 184,  18,
  158.        144,  13, 240,  28,  79, 173,  94, 160, 104, 143, 195,
  159.         28, 209, 207, 241, 145, 179, 168, 193, 173,  47,  47,
  160.         34,  24, 190,  14,  23, 119, 234, 117,  45, 254, 139,
  161.          2,  31, 161, 229, 160, 204,  15, 181, 111, 116, 232,
  162.         24, 172, 243, 214, 206, 137, 226, 153, 180, 168,  79,
  163.        224, 253,  19, 224, 183, 124, 196,  59, 129, 210, 173,
  164.        168, 217,  22,  95, 162, 102, 128, 149, 119,   5, 147,
  165.        204, 115,  20,  33,  26,  20, 119, 230, 173,  32, 101,
  166.        119, 181, 250, 134, 199,  84,  66, 245, 251, 157,  53,
  167.        207, 235, 205, 175,  12, 123,  62, 137, 160, 214,  65,
  168.         27, 211, 174,  30, 126,  73,   0,  37,  14,  45,  32,
  169.        113, 179,  94,  34, 104,   0, 187,  87, 184, 224, 175,
  170.         36, 100,  54, 155, 240,   9, 185,  30,  85,  99, 145,
  171.         29,  89, 223, 166, 170, 120, 193,  67, 137, 217,  90,
  172.         83, 127,  32, 125,  91, 162,   2, 229, 185, 197, 131,
  173.         38,   3, 118,  98, 149, 207, 169,  17, 200,  25, 104,
  174.         78, 115,  74,  65, 179,  71,  45, 202, 123,  20, 169,
  175.         74,  27,  81,   0,  82, 154,  83,  41,  21, 214,  15,
  176.         87,  63, 188, 155, 198, 228,  43,  96, 164, 118, 129,
  177.        230, 116,   0,   8, 186, 111, 181,  87,  27, 233,  31,
  178.        242, 150, 236, 107,  42,  13, 217,  21, 182,  99, 101,
  179.         33, 231, 185, 249, 182, 255,  52,   5,  46, 197, 133,
  180.         86, 100,  83, 176,  45,  93, 169, 159, 143, 161,   8,
  181.        186,  71, 153, 110, 133,   7, 106,  75, 122, 112, 233,
  182.        181, 179,  41,  68, 219, 117,   9,  46, 196,  25,  38,
  183.         35, 173, 110, 166, 176,  73, 167, 223, 125, 156, 238,
  184.         96, 184, 143, 237, 178, 102, 236, 170, 140, 113, 105,
  185.        154,  23, 255,  86, 100,  82, 108, 194, 177, 158, 225,
  186.         25,  54,   2, 165, 117,   9,  76,  41, 160,  89,  19,
  187.         64, 228,  24,  58,  62,  63,  84, 152, 154,  91,  66,
  188.        157, 101, 107, 143, 228, 214, 153, 247,  63, 214, 161,
  189.        210, 156,   7, 239, 232,  48, 245,  77,  45,  56, 230,
  190.        240,  37,  93, 193,  76, 221,  32, 134, 132, 112, 235,
  191.         38,  99, 130, 233, 198,   2,  30, 204,  94,   9, 104,
  192.        107,  63,  62, 186, 239, 201,  60, 151,  24,  20, 107,
  193.        106, 112, 161, 104, 127,  53, 132,  82, 160, 226, 134,
  194.        183, 156,  83,   5, 170,  80,   7,  55,  62,   7, 132,
  195.         28, 127, 222, 174,  92, 142, 125,  68, 236,  87,  22,
  196.        242, 184, 176,  58, 218,  55, 240,  80,  12,  13, 240,
  197.         28,  31,   4,   2,   0, 179, 255, 174,  12, 245,  26,
  198.         60, 181, 116, 178,  37, 131, 122,  88, 220,   9,  33,
  199.        189, 209, 145,  19, 249, 124, 169,  47, 246, 148,  50,
  200.         71, 115,  34, 245,  71,   1,  58, 229, 229, 129,  55,
  201.        194, 218, 220, 200, 181, 118,  52, 154, 243, 221, 167,
  202.        169,  68,  97,  70,  15, 208,   3,  14, 236, 200, 199,
  203.         62, 164, 117,  30,  65, 226,  56, 205, 153,  59, 234,
  204.         14,  47,  50, 128, 187, 161,  24,  62, 179,  49,  78,
  205.         84, 139,  56,  79, 109, 185,   8, 111,  66,  13,   3,
  206.        246,  10,   4, 191,  44, 184,  18, 144,  36, 151, 124,
  207.        121,  86, 121, 176, 114, 188, 175, 137, 175, 222, 154,
  208.        119,  31, 217, 147,   8,  16, 179, 139, 174,  18, 220,
  209.        207,  63,  46,  85,  18, 114,  31,  46, 107, 113,  36,
  210.         80,  26, 221, 230, 159, 132, 205, 135, 122,  88,  71,
  211.         24, 116,   8, 218,  23, 188, 159, 154, 188, 233,  75,
  212.        125, 140, 236, 122, 236,  58, 219, 133,  29, 250,  99,
  213.          9,  67, 102, 196, 100, 195, 210, 239,  28,  24,  71,
  214.         50,  21, 217,   8, 221,  67,  59,  55,  36, 194, 186,
  215.         22,  18, 161,  77,  67,  42, 101, 196,  81,  80, 148,
  216.          0,   2,  19,  58, 228, 221, 113, 223, 248, 158,  16,
  217.         49,  78,  85, 129, 172, 119, 214,  95,  17,  25, 155,
  218.          4,  53,  86, 241, 215, 163, 199, 107,  60,  17,  24,
  219.         59,  89,  36, 165,   9, 242, 143, 230, 237, 151, 241,
  220.        251, 250, 158, 186, 191,  44,  30,  21,  60, 110, 134,
  221.        227,  69, 112, 234, 233, 111, 177, 134,  14,  94,  10,
  222.         90,  62,  42, 179, 119,  31, 231,  28,  78,  61,   6,
  223.        250,  41, 101, 220, 185, 153, 231,  29,  15, 128,  62,
  224.        137, 214,  82, 102, 200,  37,  46,  76, 201, 120, 156,
  225.         16, 179, 106, 198,  21,  14, 186, 148, 226, 234, 120,
  226.        165, 252,  60,  83,  30,  10,  45, 244, 242, 247,  78,
  227.        167,  54,  29,  43,  61,  25,  57,  38,  15,  25, 194,
  228.        121,  96,  82,  35, 167,   8, 247,  19,  18, 182, 235,
  229.        173, 254, 110, 234, 195,  31, 102, 227, 188,  69, 149,
  230.        166, 123, 200, 131, 177, 127,  55, 209,   1, 140, 255,
  231.         40, 195,  50, 221, 239, 190, 108,  90, 165, 101,  88,
  232.         33, 133, 104, 171, 152,   2, 238, 206, 165,  15, 219,
  233.         47, 149,  59,  42, 239, 125, 173,  91, 110,  47, 132,
  234.         21,  33, 182,  40,  41,   7,  97, 112, 236, 221,  71,
  235.        117,  97, 159,  21,  16,  19, 204, 168,  48, 235,  97,
  236.        189, 150,   3,  52, 254,  30, 170,   3,  99, 207, 181,
  237.        115,  92, 144,  76, 112, 162,  57, 213, 158, 158,  11,
  238.        203, 170, 222,  20, 238, 204, 134, 188,  96,  98,  44,
  239.        167, 156, 171,  92, 171, 178, 243, 132, 110, 100, 139,
  240.         30, 175,  25, 189, 240, 202, 160,  35, 105, 185, 101,
  241.         90, 187,  80,  64, 104,  90,  50,  60,  42, 180, 179,
  242.         49, 158, 233, 213, 192,  33, 184, 247, 155,  84,  11,
  243.         25, 135,  95, 160, 153, 149, 247, 153, 126,  98,  61,
  244.        125, 168, 248,  55, 136, 154, 151, 227,  45, 119,  17,
  245.        237, 147,  95,  22, 104,  18, 129,  14,  53, 136,  41,
  246.        199, 230,  31, 214, 150, 222, 223, 161, 120,  88, 186,
  247.        153,  87, 245, 132, 165,  27,  34, 114,  99, 155, 131,
  248.        195, 255,  26, 194,  70, 150, 205, 179,  10, 235,  83,
  249.         46,  48,  84, 143, 217,  72, 228, 109, 188,  49,  40,
  250.         88, 235, 242, 239,  52, 198, 255, 234, 254,  40, 237,
  251.         97, 238, 124,  60, 115,  93,  74,  20, 217, 232, 100,
  252.        183, 227,  66,  16,  93,  20,  32,  62,  19, 224,  69,
  253.        238, 226, 182, 163, 170, 171, 234, 219, 108,  79,  21,
  254.        250, 203,  79, 208, 199,  66, 244,  66, 239, 106, 187,
  255.        181, 101,  79,  59,  29,  65, 205,  33,   5, 216,  30,
  256.        121, 158, 134, 133,  77, 199, 228,  75,  71, 106,  61,
  257.        129,  98,  80, 207,  98, 161, 242,  91, 141,  38,  70,
  258.        252, 136, 131, 160, 193, 199, 182, 163, 127,  21,  36,
  259.        195, 105, 203, 116, 146,  71, 132, 138,  11,  86, 146,
  260.        178, 133,   9,  91, 191,   0, 173,  25,  72, 157,  20,
  261.         98, 177, 116,  35, 130,  14,   0,  88,  66, 141,  42,
  262.         12,  85, 245, 234,  29, 173, 244,  62,  35,  63, 112,
  263.         97,  51, 114, 240, 146, 141, 147, 126,  65, 214,  95,
  264.        236, 241, 108,  34,  59, 219, 124, 222,  55,  89, 203,
  265.        238, 116,  96,  64, 133, 242, 167, 206, 119,  50, 110,
  266.        166,   7, 128, 132,  25, 248,  80, 158, 232, 239, 216,
  267.         85,  97, 217, 151,  53, 169, 105, 167, 170, 197,  12,
  268.          6, 194,  90,   4, 171, 252, 128,  11, 202, 220, 158,
  269.         68, 122,  46, 195,  69,  52, 132, 253, 213, 103,   5,
  270.         14,  30, 158, 201, 219, 115, 219, 211,  16,  85, 136,
  271.        205, 103,  95, 218, 121, 227, 103,  67,  64, 197, 196,
  272.         52, 101, 113,  62,  56, 216,  61,  40, 248, 158, 241,
  273.        109, 255,  32,  21,  62,  33, 231, 143, 176,  61,  74,
  274.        230, 227, 159,  43, 219, 131, 173, 247, 233,  61,  90,
  275.        104, 148, 129,  64, 247, 246,  76,  38,  28, 148, 105,
  276.         41,  52,  65,  21,  32, 247, 118,   2, 212, 247, 188,
  277.        244, 107,  46, 212, 162,   0, 104, 212,   8,  36, 113,
  278.         51,  32, 244, 106,  67, 183, 212, 183,  80,   0,  97,
  279.        175,  30,  57, 246,  46, 151,  36,  69,  70,  20,  33,
  280.         79, 116, 191, 139, 136,  64,  77, 149, 252,  29, 150,
  281.        181, 145, 175, 112, 244, 221, 211, 102, 160,  47,  69,
  282.        191, 188,   9, 236,   3, 189, 151, 133, 127, 172, 109,
  283.        208,  49, 203, 133,   4, 150, 235,  39, 179,  85, 253,
  284.         57,  65, 218,  37,  71, 230, 171, 202,  10, 154,  40,
  285.         80, 120,  37,  83,   4,  41, 244,  10,  44, 134, 218,
  286.        233, 182, 109, 251, 104, 220,  20,  98, 215,  72, 105,
  287.          0, 104,  14, 192, 164,  39, 161, 141, 238,  79,  63,
  288.        254, 162, 232, 135, 173, 140, 181, 140, 224,   6, 122,
  289.        244, 214, 182, 170, 206,  30, 124, 211,  55,  95, 236,
  290.        206, 120, 163, 153,  64, 107,  42,  66,  32, 254, 158,
  291.         53, 217, 243, 133, 185, 238,  57, 215, 171,  59,  18,
  292.         78, 139,  29, 201, 250, 247,  75, 109,  24,  86,  38,
  293.        163, 102,  49, 234, 227, 151, 178,  58, 110, 250, 116,
  294.        221,  91,  67,  50, 104,  65, 231, 247, 202, 120,  32,
  295.        251, 251,  10, 245,  78, 216, 254, 179, 151,  69,  64,
  296.         86, 172, 186,  72, 149,  39,  85,  83,  58,  58,  32,
  297.        131, 141, 135, 254, 107, 169, 183, 208, 150, 149,  75,
  298.         85, 168, 103, 188, 161,  21, 154,  88, 204, 169,  41,
  299.         99, 153, 225, 219,  51, 166,  42,  74,  86,  63,  49,
  300.         37, 249,  94, 244, 126,  28, 144,  41,  49, 124, 253,
  301.        248, 232,   2,   4,  39,  47, 112, 128, 187,  21,  92,
  302.          5,  40,  44, 227, 149, 193,  21,  72, 228, 198, 109,
  303.         34,  72, 193,  19,  63, 199,  15, 134, 220,   7, 249,
  304.        201, 238,  65,   4,  31,  15,  64,  71, 121, 164,  93,
  305.        136, 110,  23,  50,  95,  81, 235, 213, 155, 192, 209,
  306.        242, 188, 193, 143,  65,  17,  53, 100,  37, 123, 120,
  307.         52,  96,  42, 156,  96, 223, 248, 232, 163,  31,  99,
  308.        108,  27,  14,  18, 180, 194,   2, 225,  50, 158, 175,
  309.        102,  79, 209, 202, 209, 129,  21, 107,  35, 149, 224,
  310.         51,  62, 146, 225,  59,  36,  11,  98, 238, 190, 185,
  311.         34, 133, 178, 162,  14, 230, 186,  13, 153, 222, 114,
  312.         12, 140,  45, 162, 247,  40, 208,  18, 120,  69, 149,
  313.        183, 148, 253, 100, 125,   8,  98, 231, 204, 245, 240,
  314.         84,  73, 163, 111, 135, 125,  72, 250, 195, 157, 253,
  315.         39, 243,  62, 141,  30,  10,  71,  99,  65, 153,  46,
  316.        255, 116,  58, 111, 110, 171, 244, 248, 253,  55, 168,
  317.         18, 220,  96, 161, 235, 221, 248, 153,  27, 225,  76,
  318.        219, 110, 107,  13, 198, 123,  85,  16, 109, 103,  44,
  319.         55,  39, 101, 212,  59, 220, 208, 232,   4, 241,  41,
  320.         13, 199, 204,   0, 255, 163, 181,  57,  15, 146, 105,
  321.         15, 237,  11, 102, 123, 159, 251, 206, 219, 125, 156,
  322.        160, 145, 207,  11, 217,  21,  94, 163, 187,  19,  47,
  323.        136,  81,  91, 173,  36, 123, 148, 121, 191, 118,  59,
  324.        214, 235,  55,  57,  46, 179, 204,  17,  89, 121, 128,
  325.         38, 226, 151, 244,  46,  49,  45, 104,  66, 173, 167,
  326.        198, 106,  43,  59,  18, 117,  76, 204, 120,  46, 241,
  327.         28, 106,  18,  66,  55, 183, 146,  81, 231,   6, 161,
  328.        187, 230,  75, 251,  99,  80,  26, 107,  16,  24,  17,
  329.        202, 237, 250,  61,  37, 189, 216, 226, 225, 195, 201,
  330.         68,  66,  22,  89,  10,  18,  19, 134, 217,  12, 236,
  331.        110, 213, 171, 234,  42, 100, 175, 103,  78, 218, 134,
  332.        168,  95, 190, 191, 233, 136, 100, 228, 195, 254, 157,
  333.        188, 128,  87, 240, 247, 192, 134,  96, 120, 123, 248,
  334.         96,   3,  96,  77, 209, 253, 131,  70, 246,  56,  31,
  335.        176, 119,  69, 174,   4, 215,  54, 252, 204, 131,  66,
  336.        107,  51, 240,  30, 171, 113, 176, 128,  65, 135,  60,
  337.          0,  94,  95, 119, 160,  87, 190, 189, 232, 174,  36,
  338.         85,  70,  66, 153, 191,  88,  46,  97,  78,  88, 244,
  339.        143, 242, 221, 253, 162, 244, 116, 239,  56, 135, 137,
  340.        189, 194,  83, 102, 249, 195, 200, 179, 142, 116, 180,
  341.        117, 242,  85,  70, 252, 217, 185, 122, 235,  38,  97,
  342.        139,  29, 223, 132, 132, 106,  14, 121, 145,  95, 149,
  343.        226,  70, 110,  89, 142,  32, 180,  87, 112, 140, 213,
  344.         85, 145, 201,   2, 222,  76, 185,  11, 172, 225, 187,
  345.        130,   5, 208,  17, 168,  98,  72, 117, 116, 169, 158,
  346.        183, 127,  25, 182, 224, 169, 220,   9, 102,  45,   9,
  347.        161, 196,  50,  70,  51, 232,  90,  31,   2,   9, 240,
  348.        190, 140,  74, 153, 160,  37,  29, 110, 254,  16,  26,
  349.        185,  61,  29,  11, 165, 164, 223, 161, 134, 242,  15,
  350.         40, 104, 241, 105, 220, 183, 218, 131,  87,  57,   6,
  351.        254, 161, 226, 206, 155,  79, 205, 127,  82,  80,  17,
  352.         94,   1, 167,   6, 131, 250, 160,   2, 181, 196,  13,
  353.        230, 208,  39, 154, 248, 140,  39, 119,  63, 134,  65,
  354.        195,  96,  76,   6,  97, 168,   6, 181, 240,  23, 122,
  355.         40, 192, 245, 134, 224,   0,  96,  88, 170,  48, 220,
  356.        125,  98,  17, 230, 158, 215,  35,  56, 234,  99,  83,
  357.        194, 221, 148, 194, 194,  22,  52, 187, 203, 238,  86,
  358.        144, 188, 182, 222, 235, 252, 125, 161, 206,  89,  29,
  359.        118, 111,   5, 228,   9,  75, 124,   1, 136,  57, 114,
  360.         10,  61, 124, 146, 124,  36, 134, 227, 114,  95, 114,
  361.         77, 157, 185,  26, 193,  91, 180, 211, 158, 184, 252,
  362.        237,  84,  85, 120,   8, 252, 165, 181, 216,  61, 124,
  363.        211,  77, 173,  15, 196,  30,  80, 239,  94, 177,  97,
  364.        230, 248, 162, 133,  20, 217, 108,  81,  19,  60, 111,
  365.        213, 199, 231,  86, 225,  78, 196,  54,  42, 191, 206,
  366.        221, 198, 200,  55, 215, 154,  50,  52, 146,  99, 130,
  367.         18, 103,  14, 250, 142,  64,  96,   0, 224,  58,  57,
  368.        206,  55, 211, 250, 245, 207, 171, 194, 119,  55,  90,
  369.        197,  45,  27,  92, 176, 103, 158,  79, 163,  55,  66,
  370.        211, 130,  39,  64, 153, 188, 155, 190, 213,  17, 142,
  371.        157, 191,  15, 115,  21, 214,  45,  28, 126, 199,   0,
  372.        196, 123, 183, 140,  27, 107,  33, 161, 144,  69, 178,
  373.        110, 177, 190, 106,  54, 110, 180,  87,  72, 171,  47,
  374.        188, 148, 110, 121, 198, 163, 118, 210, 101,  73, 194,
  375.        200,  83,  15, 248, 238,  70, 141, 222, 125, 213, 115,
  376.         10,  29,  76, 208,  77, 198,  41,  57, 187, 219, 169,
  377.        186,  70,  80, 172, 149,  38, 232, 190,  94, 227,   4,
  378.        161, 250, 213, 240, 106,  45,  81, 154,  99, 239, 140,
  379.        226, 154, 134, 238,  34, 192, 137, 194, 184,  67,  36,
  380.         46, 246, 165,  30,   3, 170, 156, 242, 208, 164, 131,
  381.        192,  97, 186, 155, 233, 106,  77, 143, 229,  21,  80,
  382.        186, 100,  91, 214,  40,  38, 162, 249, 167,  58,  58,
  383.        225,  75, 169, 149, 134, 239,  85,  98, 233, 199,  47,
  384.        239, 211, 247,  82, 247, 218,  63,   4, 111, 105, 119,
  385.        250,  10,  89, 128, 228, 169,  21, 135, 176, 134,   1,
  386.        155,   9, 230, 173,  59,  62, 229, 147, 233, 144, 253,
  387.         90, 158,  52, 215, 151,  44, 240, 183, 217,   2,  43,
  388.        139,  81, 150, 213, 172,  58,   1, 125, 166, 125, 209,
  389.        207,  62, 214, 124, 125,  45,  40,  31, 159,  37, 207,
  390.        173, 242, 184, 155,  90, 214, 180, 114,  90, 136, 245,
  391.         76, 224,  41, 172, 113, 224,  25, 165, 230,  71, 176,
  392.        172, 253, 237, 147, 250, 155, 232, 211, 196, 141,  40,
  393.         59,  87, 204, 248, 213, 102,  41, 121,  19,  46,  40,
  394.        120,  95,   1, 145, 237, 117,  96,  85, 247, 150,  14,
  395.         68, 227, 211,  94, 140,  21,   5, 109, 212, 136, 244,
  396.        109, 186,   3, 161,  97,  37,   5, 100, 240, 189, 195,
  397.        235, 158,  21,  60, 144,  87, 162, 151,  39,  26, 236,
  398.        169,  58,   7,  42,  27,  63, 109, 155,  30,  99,  33,
  399.        245, 245, 156, 102, 251,  38, 220, 243,  25, 117,  51,
  400.        217,  40, 177,  85, 253, 245,   3,  86,  52, 130, 138,
  401.        186,  60, 187,  40,  81, 119,  17, 194,  10, 217, 248,
  402.        171, 204,  81, 103, 204, 173, 146,  95,  77, 232,  23,
  403.         81,  56,  48, 220, 142,  55, 157,  88,  98, 147,  32,
  404.        249, 145, 234, 122, 144, 194, 251,  62, 123, 206,  81,
  405.         33, 206, 100, 119,  79, 190,  50, 168, 182, 227, 126,
  406.        195,  41,  61,  70,  72, 222,  83, 105, 100,  19, 230,
  407.        128, 162, 174,   8,  16, 221, 109, 178,  36, 105, 133,
  408.         45, 253,   9,   7,  33, 102, 179, 154,  70,  10, 100,
  409.         69, 192, 221,  88, 108, 222, 207,  28,  32, 200, 174,
  410.         91, 190, 247, 221,  27,  88, 141,  64, 204, 210,   1,
  411.        127, 107, 180, 227, 187, 221, 162, 106, 126,  58,  89,
  412.        255,  69,  62,  53,  10,  68, 188, 180, 205, 213, 114,
  413.        234, 206, 168, 250, 100, 132, 187, 141, 102,  18, 174,
  414.        191,  60, 111,  71, 210, 155, 228,  99,  84,  47,  93,
  415.        158, 174, 194, 119,  27, 246,  78,  99, 112, 116,  14,
  416.         13, 141, 231,  91,  19,  87, 248, 114,  22, 113, 175,
  417.         83, 125,  93,  64,  64, 203,   8,  78, 180, 226, 204,
  418.         52, 210,  70, 106,   1,  21, 175, 132, 225, 176,   4,
  419.         40, 149, 152,  58,  29,   6, 184, 159, 180, 206, 110,
  420.        160,  72, 111,  63,  59, 130,  53,  32, 171, 130,   1,
  421.         26,  29,  75,  39, 114,  39, 248,  97,  21,  96, 177,
  422.        231, 147,  63, 220, 187,  58, 121,  43,  52,  69,  37,
  423.        189, 160, 136,  57, 225,  81, 206, 121,  75,  47,  50,
  424.        201, 183, 160,  31, 186, 201, 224,  28, 200, 126, 188,
  425.        199, 209, 246, 207,   1,  17, 195, 161, 232, 170, 199,
  426.         26, 144, 135,  73, 212,  79, 189, 154, 208, 218, 222,
  427.        203, 213,  10, 218,  56,   3,  57, 195,  42, 198, 145,
  428.         54, 103, 141, 249,  49, 124, 224, 177,  43,  79, 247,
  429.        158,  89, 183,  67, 245, 187,  58, 242, 213,  25, 255,
  430.         39, 217,  69, 156, 191, 151,  34,  44,  21, 230, 252,
  431.         42,  15, 145, 252, 113, 155, 148,  21,  37, 250, 229,
  432.        147,  97, 206, 182, 156, 235, 194, 168, 100,  89,  18,
  433.        186, 168, 209, 182, 193,   7,  94, 227,   5, 106,  12,
  434.         16, 210,  80, 101, 203,   3, 164,  66, 224, 236, 110,
  435.         14,  22, 152, 219,  59,  76, 152, 160, 190,  50, 120,
  436.        233, 100, 159,  31, 149,  50, 224, 211, 146, 223, 211,
  437.        160,  52,  43, 137, 113, 242,  30,  27,  10, 116,  65,
  438.         75, 163,  52, 140, 197, 190, 113,  32, 195, 118,  50,
  439.        216, 223,  53, 159, 141, 155, 153,  47,  46, 230,  11,
  440.        111,  71,  15, 227, 241,  29, 229,  76, 218,  84,  30,
  441.        218, 216, 145, 206,  98, 121, 207, 205,  62, 126, 111,
  442.         22,  24, 177, 102, 253,  44,  29,   5, 132, 143, 210,
  443.        197, 246, 251,  34, 153, 245,  35, 243,  87, 166,  50,
  444.        118,  35, 147, 168,  53,  49,  86, 204, 205,   2, 172,
  445.        240, 129,  98,  90, 117, 235, 181, 110,  22,  54, 151,
  446.        136, 210, 115, 204, 222, 150,  98, 146, 129, 185,  73,
  447.        208,  76,  80, 144,  27, 113, 198,  86,  20, 230, 198,
  448.        199, 189,  50, 122,  20,  10,  69, 225, 208,   6, 195,
  449.        242, 123, 154, 201, 170,  83, 253,  98, 168,  15,   0,
  450.        187,  37, 191, 226,  53, 189, 210, 246, 113,  18, 105,
  451.          5, 178,   4,   2,  34, 182, 203, 207, 124, 205, 118,
  452.        156,  43,  83,  17,  62, 192,  22,  64, 227, 211,  56,
  453.        171, 189,  96,  37,  71, 173, 240, 186,  56,  32, 156,
  454.        247,  70, 206, 118, 119, 175, 161, 197,  32, 117,  96,
  455.         96, 133, 203, 254,  78, 138, 232, 141, 216, 122, 170,
  456.        249, 176,  76, 249, 170, 126,  25,  72, 194,  92,   2,
  457.        251, 138, 140,   1, 195, 106, 228, 214, 235, 225, 249,
  458.        144, 212, 248, 105, 166,  92, 222, 160,  63,   9,  37,
  459.         45, 194,   8, 230, 159, 183,  78,  97,  50, 206, 119,
  460.        226,  91,  87, 143, 223, 227,  58, 195, 114, 230, 184,
  461.         58, 203,   2,  32,   2,  57, 122, 110, 198, 251,  91,
  462.        255, 207, 212, 221,  76, 191,  94, 209, 244,  63, 229,
  463.        130,  62, 244, 232,  35,  45,  21,  42, 240, 231,  24,
  464.        201, 112,  89, 189, 152,  32,  31,  74, 157,  98, 231,
  465.        165,  41, 186, 137, 225,  36, 141,  59, 248, 134,  86,
  466.        197,  17,  77,  14, 188,  76, 238,  22,   3,  77, 138,
  467.         57,  32, 228, 120, 130, 233, 174, 143, 189, 227, 171,
  468.        220,  31, 109, 165,  30,  82,  93, 178, 186, 225,   1,
  469.        248, 110, 122, 109, 156, 104, 169,  39,   8, 252, 217,
  470.         41,  60, 188,  12, 176,  60, 134, 248, 168, 173,  44,
  471.         47,   0,  66,  78, 235, 202, 203,  69,  45, 137, 204,
  472.        113, 252, 213, 156, 127, 145, 127,   6,  34, 188, 109,
  473.        138,   8, 177, 131,  77,  33,  50, 104, 132, 202, 130,
  474.        227, 170, 203, 243, 119, 134, 242, 250,  44, 171, 110,
  475.         61, 206,  83,  90, 209, 242,  10, 198,   7, 198, 184,
  476.        225,  79,  94, 180,  56, 142, 119,  80,  20, 166, 101,
  477.        102, 101, 247, 182,  74,  67, 228, 186,  56,  61,   1,
  478.        178, 228,  16, 121, 142, 178, 152, 111, 144, 158,  12,
  479.        164,  31, 123,  55, 119,  44,  18,  96,  48, 133,   8,
  480.        135,  24, 196, 231, 209, 189,  64, 101, 255, 206, 131,
  481.        146, 253, 138, 170,  54, 209,  43, 180, 200, 201, 208,
  482.        153,  79, 176, 183,  20, 249, 104,  24, 249, 165,  57,
  483.        152, 160, 161, 120, 198,  38, 132, 168,  30, 138, 233,
  484.        114, 246, 184,  66,  94, 182, 122,  41, 212, 134,  85,
  485.         27, 215,  25, 175,  50, 193, 137, 213,  20,  85,   5,
  486.        220, 129, 213,  62,  72,  66,  78, 218, 183, 150, 239,
  487.         70, 160,  73, 143,   3, 102, 125, 238, 222,   3, 172,
  488.         10, 179, 196, 151, 115,  61,  83,  22, 168, 145,  48,
  489.        168, 143, 204, 150,   4,  68,  10, 206, 235, 137,  58,
  490.        119,  37, 184,  43,  14,  30, 246, 157,  48,  42,  92,
  491.        142, 231, 184,  77, 239,  90,  49, 176, 150, 201, 235,
  492.        248, 141,  81,  45, 120, 142, 126,  64,   2, 238, 135,
  493.        224,  42, 246, 195,  88, 161, 187,   2, 232, 215, 175,
  494.        223, 159, 176, 231, 121,  14, 148,  42,  59,  60,  26,
  495.        186, 198, 255, 167, 175, 157, 247, 150, 249,  50,  27,
  496.        185, 148,   1, 116, 168, 168, 237,  34,  22,  44, 207,
  497.        241, 187, 153, 218, 168, 213,  81, 164, 213, 228,  75,
  498.        236, 221, 227, 236, 168,  13, 197,   9,   3, 147, 238,
  499.        242, 114,  82,  61,  49, 212, 142,  58,  28,  34,  78,
  500.        182,  94,  96,  82, 195, 164,  33,   9, 195,  47,   5,
  501.         46, 227, 136, 237, 159, 126, 169, 145, 198,  47, 151,
  502.        119, 181,  91, 160,  21,  12, 188, 163,  58, 236, 101,
  503.         37, 223,  49, 131, 131,  67, 169, 206,  38, 147,  98,
  504.        173, 139,   1,  52,  20,  11, 141, 245, 207, 129,  30,
  505.        159, 245,  89,  22, 127,   5, 100,  56,  18, 244, 224,
  506.         88, 138,  82, 176, 203, 184, 233,  68, 239,  91,  22,
  507.        163, 115, 196, 237, 161, 125, 252, 254, 234, 245,  75,
  508.        203, 190, 135, 115, 227, 210, 197,  49, 220, 208,  85,
  509.        196, 103,  41,  82, 119,  79,  58,  87, 202, 107, 192,
  510.         70, 125,  58,  59,  36, 119, 132,  37, 183, 153,  30,
  511.        154, 221, 130,  92,  38, 228,  82, 200, 238, 252, 172,
  512.        222,  30, 132, 131,  58, 243,  97,  33,  29,   3,  23,
  513.         50, 193,  49, 204, 173, 178,  71, 230,   6, 190, 140,
  514.        113,  43,  57, 241, 136, 180, 239,  57,  58, 159, 205,
  515.        197, 197, 117,  81, 105,  31, 246, 153,  79,  57, 130,
  516.        156, 176,  17,   1, 101, 115,  51,  67, 203, 235,  97,
  517.        211, 208, 180,  68, 243,  10, 239, 168, 174, 115, 117,
  518.         42,  58,  28, 157, 180, 183,   9,  20, 214, 171,  37,
  519.         12, 133,  59, 115,  40,  73,  95, 148, 143, 210, 164,
  520.        237, 142, 108, 247,  81, 228, 195,  32, 187, 117, 217,
  521.        202, 160, 179, 139, 165,  98,  98,  78, 132, 176,  63,
  522.        238, 168,   7, 110, 116, 160, 127, 229, 128,  57, 224,
  523.         12,  54, 255, 218, 248,   3, 115,  19,  88, 185, 230,
  524.        113, 185, 218, 196, 206,  28, 178,  91,  16, 237,  77,
  525.        211, 213, 177, 252, 242, 180, 128,  70,  52, 245, 121,
  526.         37, 234, 196,   0, 169, 172,  85, 234, 114, 137,  50,
  527.        223,   6,   4,  29,   5,  93,  49, 245,   2, 197,  57,
  528.        194, 227,  43, 137, 217, 219,  91, 204,  10, 152, 192,
  529.         91, 253, 111,  27,  37,   6,  34,  46,  33, 190,  14,
  530.         96, 151,  59,   4, 236, 213,  74, 103, 181,  79, 230,
  531.         56, 166, 237, 102,  21, 152,  26, 145,  10,  93, 146,
  532.        146, 141, 172, 111, 198, 151, 231,  60,  99, 173,  69,
  533.        110, 223,  95,  69, 122, 129,  69,  81, 135,  90, 100,
  534.        205,  48, 153, 241, 105, 181, 241, 138, 140, 115, 238,
  535.         11,  94,  87,  54, 143, 108, 121, 244, 187, 122,  89,
  536.         89,  38, 170, 180, 158, 198, 138, 200, 252, 251, 128,
  537.          0);
  538.  
  539.  
  540.  
  541. PROCEDURE InitBlowFish(Key:STRING; VAR P:PArray; VAR S:SBox);
  542.  
  543. {         Initialization of a key is needed before performing any encryption
  544.           or decryption of data.
  545. }
  546.  
  547. PROCEDURE BlowEncrypt(VAR Xl,Xr:LongInt;P:PArray;S:SBox);
  548. PROCEDURE BlowDecrypt(VAR Xl,Xr:LongInt;P:PArray;S:SBox);
  549.  
  550. {         These two should speak for themselves, Xl and Xr are the first and
  551.           the second 32 bits of data you want to encrypt or decrypt, P and S
  552.           are the P-array and the S-boxes which you currently are using.
  553.  
  554.           Note that the data is not copied or stored before the Blowfish al-
  555.           gorithm  is applied to it  and  that you have to perform a loop in
  556.           order to encrypt or decrypt more data than 8 bytes.
  557. }
  558.  
  559. IMPLEMENTATION
  560.  
  561. PROCEDURE InitBlowFish(Key:STRING; VAR P:PArray; VAR S:SBox);
  562.  
  563. VAR
  564.     i,j,k,l:BYTE;
  565.     Data,Data1,Datar:LongInt;
  566.  
  567. BEGIN
  568.      { Initialize first the P-array
  569.      }
  570.      FOR i:=1 TO 17 DO P[i]:=RandomSeed[i-1];
  571.      l:=17;
  572.      {                              and then the four S-boxes, in order,
  573.        with a fixed random string. This string consists of the hexadecimal
  574.        digits of Pi.
  575.      }
  576.      FOR i:=1 TO 4 DO
  577.       BEGIN
  578.            FOR j:=0 TO 255 DO S[i,j]:=RandomSeed[l+j];
  579.            l:=l+(i*256);
  580.       END;
  581.      j:=1;
  582.      { XOR P1 with the first 32 bits of the key, XOR P2 with the second 32
  583.        bits of the key, and so on for all bits of the key (up to P18). Cycle
  584.        throught the key bits repeatedly until the entire P-array has been
  585.        XORed.
  586.      }
  587.      FOR i:=1 TO 18 DO
  588.       BEGIN
  589.        Data:=$00000000;
  590.        FOR k:=1 TO 4 DO
  591.         BEGIN
  592.          Data:=((Data SHL 8) OR Ord(Key[j]));
  593.          Inc(j);
  594.          IF j>Length(Key) THEN j:=1;
  595.         END;
  596.        P[i]:=P[i] XOR Data;
  597.       END;
  598.      { Encrypt the all-zero string using the Blowfish algorithm using the
  599.        subkeys described in steps #1 and #2 and replace the elements of the
  600.        P-array with the output of this process.
  601.      }
  602.      Data1:=$00000000;
  603.      Datar:=$00000000;
  604.      FOR i:=1 TO 18 DO
  605.      BEGIN
  606.       BlowEncrypt(Data1,Datar,P,S);
  607.       P[i]:=Data1;
  608.       P[i+1]:=Datar;
  609.       Inc(i);
  610.      END;
  611.     { Continue the process, replacing the elements of the four S-boxes in
  612.       order, with the output of the continuously changing Blowfish algorithm.
  613.     }
  614.     FOR j:=1 TO 4 DO
  615.      BEGIN
  616.       FOR i:=0 TO 255 DO
  617.        BEGIN
  618.         BlowEnCrypt(Data1,Datar,P,S);
  619.         S[j,i]:=Data1;
  620.         S[j,i+1]:=Datar;
  621.         Inc(i);
  622.         END;
  623.       END;
  624.      { In total, 521 iterations are required to generate all required subkeys.
  625.        Applications can store the subkeys rather tan re-executing this deri-
  626.        vation on process.
  627.      }
  628. END;
  629.  
  630. {$IFNDEF Include_Asm}
  631.  
  632. FUNCTION F(Input:LongInt;S:SBox):LongInt;
  633.  VAR
  634.     PInput:PLArray;
  635.  
  636.  BEGIN
  637.       PInput:=@Input;
  638.       F:=((S[1,PInput^[0]] + S[2,PInput^[1]] {MOD HighestNumber})
  639.        XOR S[3,PInput^[2]]) + S[4,PInput^[3]] { MOD HighestNumber};
  640.  
  641.       { In the original article in Dr. Dobb's Journal this function included
  642.         a MOD 2^32 (which  wouldn't fit in a 32 bits integer anyway), in the
  643.         source there was a MOD (2^32 - 1) while in the C source available on
  644.         Dr. Dobb's  official FTP site on  ftp.mv.com there wasn't any MOD to
  645.         be found. After  he was asked about this phenomenon,  Bruce Schneier
  646.         stated that it was not needed anyway. In this source it is left out-
  647.         commented for completeness.
  648.       }
  649.  END;
  650.  
  651. {$ELSE}
  652.  
  653. FUNCTION F(Input:LongInt;S:SBox):LongInt; near; external;
  654.  
  655. {$L BLOWF_F.OBJ}
  656.  
  657. {$ENDIF}
  658.  
  659. {$IFNDEF Include_Asm}
  660.  
  661. PROCEDURE BlowEncrypt(VAR pXl,pXr:LongInt;P:PArray;S:SBox);
  662.  VAR
  663.     i:BYTE;
  664.     Temp,Xl,Xr:LongInt;
  665.  
  666.  BEGIN
  667.       Xl:=pXl
  668.       Xr:=pXr
  669.  
  670.       FOR i:=1 TO 16 DO
  671.        BEGIN
  672.             Xl:=Xl XOR P[i];
  673.             Xr:=F(Xl,S) XOR Xr;
  674.             Temp:=Xl;
  675.             Xl:=Xr;
  676.             Xr:=Temp;
  677.        END;
  678.       Temp:=Xl;
  679.       Xl:=Xr;
  680.       Xr:=Temp;
  681.  
  682.       Xr:=Xr XOR P[17];
  683.       Xl:=Xl XOR P[18];
  684.  
  685.       pXl:=Xl;
  686.       pXr:=Xr;
  687.  END;
  688.  
  689. {$ELSE}
  690.  
  691. PROCEDURE BlowEncrypt(VAR Xl,Xr:LongInt;P:PArray;S:SBox); external;
  692.  
  693. {$L BLOWF_E.OBJ}
  694.  
  695. {$ENDIF}
  696.  
  697. {$IFNDEF Include_Asm}
  698.  
  699. PROCEDURE BlowDecrypt(VAR pXl,pXr:LongInt;P:PArray;S:SBox);
  700.  VAR
  701.     i:BYTE;
  702.     Temp,Xl,Xr:LongInt;
  703.  
  704.  BEGIN
  705.       Xl:=pXl;
  706.       Xr:=PXr;
  707.  
  708.       FOR i:=18 DOWNTO 3 DO
  709.        BEGIN
  710.             Xl:=Xl XOR P[i];
  711.             Xr:=F(Xl,S) XOR Xr;
  712.             Temp:=Xl;
  713.             Xl:=Xr;
  714.             Xr:=Temp;
  715.        END;
  716.       Temp:=Xl;
  717.       Xl:=Xr;
  718.       Xr:=Temp;
  719.  
  720.       Xr:=Xr XOR P[2];
  721.       Xl:=Xl XOR P[1];
  722.  
  723.       pXl:=Xl;
  724.       pXr:=Xr;
  725.  END;
  726.  
  727. {$ELSE}
  728.  
  729. PROCEDURE BlowDecrypt(VAR Xl,Xr:LongInt;P:PArray;S:SBox); external;
  730.  
  731. {$L BLOWF_D.OBJ}
  732.  
  733. {$ENDIF}
  734.  
  735. END.
  736.